dialog = new Object();

dialog.init = function(obj, target) {
	eachAttr(obj, function(attr) {
		if (attr.specified) {
			if (attr.name.substring(0, 14) == "data-dialog-t-") {
				let oname = attr.name.substring(14);
				let ovalue = attr.value;
				let fname = "[data-t='"+oname+"']";
				$(target).find(fname).text(ovalue);
			} else if (attr.name.substring(0, 14) == "data-dialog-v-") {
				let oname = attr.name.substring(14);
				let ovalue = attr.value;
				let fname = "[data-v='"+toHump(oname)+"']";
				$(target).find(fname).val(ovalue);
			} else if (attr.name.substring(0, 14) == "data-dialog-p-") {
				let oname = attr.name.substring(14);
				let ovalue = attr.value;
				let fname = "[data-p='"+oname+"']";
				$(target).find(fname).attr("placeholder", ovalue);
			} else if (attr.name.substring(0, 14) == "data-dialog-a-") {
				let oname = attr.name.substring(14);
				let ovalue = attr.value;
				let fname = "[data-a='"+oname+"']";
				$(target).find(fname).attr("data-"+oname, ovalue);
			} else if (attr.name.substring(0, 19) == "data-dialog-action-") {
				let oname = attr.name.substring(19);
				let ovalue = attr.value;
				let fname = "[data-action='"+oname+"']";
				$(target).find(fname).attr("data-path", ovalue);
			}
		}
	});
};

dialog.info = function(obj) {
	dialog.init(obj, "#dialog-info");
	$("#dialog-info").modal();
};

dialog.confirm = function(obj, func) {
	try {
		obj.getObject();
		dialog.init(obj, "#dialog-confirm");
		window.confirm = ()=>func(obj);
		$("#dialog-confirm").modal();
	} catch (e) {
		if (e == 'ids不能为空') {
			toast.info("请至少选中一项");
			return false;
		} else {
			toast.info(e);
			console.info(e);
		}
	}
};

dialog.danger = function(obj, func) {
	dialog.init(obj, "#dialog-danger");
	window.danger = ()=>func(obj);
	$("#dialog-danger").modal();
};

dialog.input = function(obj, func) {
	dialog.init(obj, "#dialog-input");
	var name = $(obj).attr('data-dialog-prompt');
	window.input = ()=>{
		var value = $("#dialog-input").find("[name='inputValue']").val();
		$(obj).attr("data-obj-" + name, value);
		func(obj);
	};
	$("#dialog-input").modal();
};

dialog.target = function(obj) {
	dialog.init(obj, "#dialog-target");
	$("#dialog-target").modal();
};

dialog.show = function(obj, target) {
	try {
		obj.getObject();
		dialog.init(obj, target);
		$(target).modal();
	} catch (e) {
		if (e == 'ids不能为空') {
			toast.info("请至少选中一项");
			return false;
		} else {
			console.info(e);
		}
	}
};

dialog.url = function(obj) {
	let dialog = $("#dialog-url");
	let content = dialog.find('.modal-content');
	content.empty();
	postThis(obj, function(status, data, headers) {
		content.append(data);
		return false;
	}, function(status, data, headers) {
		dialog.modal('hide');
		toast.failure(getMessage(headers), reload);
		return false;
	});
	dialog.modal();
	return false;
};

dialog.close = function(obj) {
	$(obj).modal('hide');
};

dialog.ajaxSuccess = function(obj, status, msg, data) {
	$(obj).parents('.modal').modal('hide');
	toast.success(msg, reload);
};

dialog.ajaxFailure = function(obj, status, msg, data) {
	var dialog = $(obj).parents('.modal');
	dialog.modal('hide');
	toast.failure(msg, ()=>dialog.modal(), 3000);
};

dialog.method = function(obj, func) {
	func(obj, dialog.ajaxSuccess, dialog.ajaxFailure);
	return false;
};

//======================
function hideModal(obj) {
	let parent = $(obj).parents('.modal');
	if (parent.length > 0) {
		parent.modal('hide');
	}
}

//==============
$(function () {
	var html = '<div class="modal fade"id="dialog-info"><div class="modal-dialog"style="margin-top: 10%"><div class="modal-content"style="border-radius: 5px"><div class="modal-header"><h3><i class="icon fas fas-info text-info"></i><span data-t="title"class="text-info"style="margin-left: 20px">提示</span></h3></div><div class="modal-body"><p data-t="msg"></p></div><div class="modal-footer"style="text-align: center;"><button class="btn btn-info"data-dismiss="modal">关闭</button></div></div></div></div><div class="modal fade"id="dialog-confirm"><div class="modal-dialog"style="margin-top: 10%"><div class="modal-content"style="border-radius: 5px"><div class="modal-header"><h3><i class="icon fas fa-exclamation-triangle text-warning"></i><span data-t="title"class="text-warning"style="margin-left: 20px">提示</span></h3></div><div class="modal-body"><p data-t="msg"></p></div><div class="modal-footer"style="text-align: center;"><button class="btn btn-default pull-left"data-dismiss="modal"style="width: 100px">取消</button><button class="btn btn-warning pull-right"data-dismiss="modal"style="width: 100px">确定</button></div></div></div></div><div class="modal fade"id="dialog-input"><div class="modal-dialog"style="margin-top: 10%"><div class="modal-content"style="border-radius: 5px"><div class="modal-header"><h3><i class="icon fas fa-edit text-success"></i><span data-t="title"class="text-success"style="margin-left: 20px">提示</span></h3></div><div class="modal-body"><label data-t="name"class="control-label"data-t="label">Email</label><input data-p="input"data-v="input"name="inputValue"type="text"class="form-control input-sm"placeholder=""autofocus="autofocus"></div><div class="modal-footer"><button class="btn btn-default pull-left"data-dismiss="modal"style="width: 100px">取消</button><button class="btn btn-success pull-right"data-dismiss="modal"style="width: 100px">确定</button></div></div></div></div><div class="modal fade"id="dialog-url"><div class="modal-dialog"style="width: 100%;"><div class="modal-content"></div></div></div>';
	$("body").children(":first").before(html);
	
	$('.modal').on('shown.bs.modal', function (event) {
		var $selectId = $("#selectId");
		if ($selectId != undefined) {
			if ($selectId[0] != undefined) {
				var selectId = $selectId[0].value;
				if (selectId == undefined || selectId == "undefined") {
					var target = $("option")[0];
					if (target != undefined) {
						target.selected=true;
					}
				} else {
					var target = $("option[value='"+selectId+"']")[0];
					if (target != undefined) {
						target.selected=true;
					}
				}
			}
		}
		var $obj = $(event.target);
		var $autofocus = $obj.find("[autofocus='autofocus']");
		if ($autofocus.length != 0) {
			$autofocus.focus();
		}
		
	});
	
	$('.modal').on('hidden.bs.modal', function (event) {
		var $autofocus = $(".content-wrapper").find("input[autofocus='autofocus']");
		if ($autofocus.length != 0) {
			$autofocus.focus();
		}
	});
	
	$('.modal').on('show.bs.modal', function () {
    	var $parent = $(this).find("[data-append]");
    	var target = $parent.attr("data-append");
    	if (target != undefined) {
        	var $target;
        	if (target.length>7 && target.substr(target.length-7,7)==":parent") {
        		target = target.substr(0,target.length-7);
        		$target = $(target).parent();
    		} else {
    			$target = $(target);
    		}
        	$parent.append($target[0]);
        	$target.removeClass("hidden");
		}
	});
	
    $('.modal').on('hide.bs.modal', function () {
    	var $parent = $(this).find("[data-append]");
    	var target = $parent.attr("data-append");
    	if (target != undefined) {
        	var $target;
        	if (target.length>7 && target.substr(target.length-7,7)==":parent") {
        		target = target.substr(0,target.length-7);
        		$target = $(target).parent();
    		} else {
    			$target = $(target);
    		}
        	$target.addClass("hidden");
        	$(document.body).prepend($target[0]);
		}
	});
	
	var info = $("#dialog-info");
	
	var confirm = $("#dialog-confirm");
	confirm.find(".btn-warning").click(function() {
		window.confirm();
	});
	confirm.keydown(function(event){
		if (event.keyCode == 13) {
			window.confirm();
		}
	});
	
	var danger = $("#dialog-danger");
	danger.find(".btn-success").click(function() {
		window.danger();
	});
	danger.keydown(function(event){
		if (event.keyCode == 13) {
			window.danger();
		}
	});
	
	var input = $("#dialog-input");
	input.find(".btn-success").click(function() {
		window.input();
	});
	input.keydown(function(event){
		if (event.keyCode == 13) {
			window.input();
		}
	});
	
	input.on('hide.bs.modal', function () {
		input.find("[name='inputValue']").val("");
    });
	input.on('shown.bs.modal', function () {
		input.find("[name='inputValue']").focus();
    });
	
	$("[data-dialog-show]").each((index, obj)=>{
		var $obj = $(obj);
        if ($obj[0].tagName == 'A') {
            $obj.attr("href", "#");
        } else {
            obj.style.cssText =  obj.style.cssText+"cursor:pointer;";
        }
		var url = $obj.attr("data-dialog-show");
		$obj.click(()=>dialog.show(obj, url));
	});
	$("[data-dialog-confirm]").each((index, obj)=>{
		var $obj = $(obj);
		if ($obj[0].tagName == 'A') {
            $obj.attr("href", "#");
        } else {
            obj.style.cssText =  obj.style.cssText+"cursor:pointer;";
        }
		$obj.click(()=>{
			var url = $obj.attr("data-dialog-confirm");
			var strs = url.split(":");
			if (strs.length == 1) {
				strs[1] = $obj.attr("data-method");
			}
			$obj.attr("data-url", strs[0]);
			$obj.attr("data-method", strs[1]);
			dialog.confirm(obj, obj=>doMethodThis(obj, defaultSuccessFunc, defaultFailureFunc, defaultExceptionFunc));
		});
	});
	$("[data-dialog-url]").each((index, obj)=>{
		var $obj = $(obj);
		var url = $obj.attr("data-dialog-url");
        if ($obj[0].tagName == 'A') {
            $obj.attr("href", "#");
        } else {
            obj.style.cssText =  obj.style.cssText+"cursor:pointer;";
        }
		$obj.removeAttr("data-dialog-url");
		$obj.attr("data-url", url);
		$obj.click(()=>dialog.url(obj));
	});
	$("[data-dialog-input]").each((index, obj)=>{
		var $obj = $(obj);
		var url = $obj.attr("data-dialog-input");
		var strs = url.split(":");
		if ($obj[0].tagName == 'A') {
            $obj.attr("href", "#");
		} else {
            obj.style.cssText =  obj.style.cssText+"cursor:pointer;";
        }
		$obj.click(()=>dialog.input(obj, obj=>doMethod(obj, strs[0], strs[1])));
	});
	
	$("[data-dialog]").each((index, obj)=>{
		let $obj = $(obj);
		if ($obj[0].tagName == 'A') {
			$obj.attr("href", "#");
		} else {
            obj.style.cssText =  obj.style.cssText+"cursor:pointer;";
        }
		let type = $obj.attr("data-dialog");
		if (type == "show") {
			$obj.click(()=>dialog.show(obj, obj.getAttribute("data-url")));
		} else if (type == "confirm") {
			$obj.click(()=>dialog.confirm(obj, obj=>doMethodThis(obj, defaultSuccessFunc, defaultFailureFunc, defaultExceptionFunc)));
		} else if (type == "url") {
			$obj.click(()=>dialog.url(obj));
		}
	});
});